﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;



using Meqs.Entity;
using Meqs.Entity.Views;
using Meqs.Logic.Interface;

namespace Meqs.Logic.Implement
{
    public class MoralityHistoryLogic : CommonLogic<MoralityHistory, int>, IMoralityHistoryLogic
    {

        #region Implementation of ICommonLogic<MoralityHistory, int>

        /// <sammury>
        /// 创建对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Create(MoralityHistory entity)
        {
            return CreateObject(entity);
        }

        /// <sammury>
        /// 更新对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Update(MoralityHistory entity)
        {
            return UpdateObject(entity);
        }

        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="entityId"></param>
        /// <returns></returns>
        public int Delete(int entityId)
        {
            return DeleteObject(entityId);
        }

        /// <summary>
        /// 删除所有对象
        /// </summary>
        /// <returns></returns>
        public int DeleteAll()
        {
            return DeleteAllObject();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entityId"></param>
        /// <returns></returns>
        public MoralityHistory FindById(int entityId)
        {
            return FindObjectById(entityId);
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <returns></returns>
        public int FindCount()
        {
            return FindObjectByCount();
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        public IList<MoralityHistory> FindAll()
        {
            return FindAllObject();
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <returns></returns>
        public int FindCount(Hashtable table)
        {
            return FindObjectByCount(table);
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        public IList<MoralityHistory> FindAll(Hashtable table)
        {
            return FindObject(table);
        }

        #endregion

        /// <summary>
        /// 获取指定班级列表里的年级
        /// </summary>
        public IList<int> GetGrade(String schoolMark, bool showHistory, IList<int> classList)
        {
            //String idList = "";
            //StringBuilder sb = new StringBuilder();
            //if(classList == null || classList.Count < 1)
            //    return null;

            //foreach (int i in classList)
            //{
            //    sb.Append(i);
            //    sb.Append(',');
            //}
            //idList = sb.ToString(0, sb.ToString().Length - 1);

            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            ht.Add("IDList", classList);
            if(!showHistory)
                ht.Add("IsEffect", "1");

            IList<int> list = this.ExecuteQueryForList<int>("MoralityHistory.FindGrade", ht);
            return list;
        }

        /// <summary>
        /// 获取当前评价状态
        /// </summary>
        public IList<MoralityState> GetState(int schoolYear, int term,String keyword, String schoolMark, IList<int> clsList)
        { 
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            ht.Add("Year", schoolYear);
            ht.Add("Term", term);
            ht.Add("IDList", clsList);
            if(keyword.Length>0)
                ht.Add("KeyWord", keyword);
            IList<MoralityState> result = this.ExecuteQueryForList<MoralityState>("MoralityHistory.GetState", ht);
            return result;
        }

        /// <summary>
        /// 获取评价历史记录
        /// </summary>
        /// <param name="schoolYear"></param>
        /// <param name="term"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public MoralityHistory GetHistoryRecordByTerm(int schoolYear, int term, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolYear", schoolYear);
            ht.Add("Term", term);
            ht.Add("SchoolMark", schoolMark);

            return FindCustomerByID<MoralityHistory>("MoralityHistory.GetHistoryRecordByTerm", ht);
        }

        public int InsertNew(string schoolMark, int schoolYear, int term, int userId)
        {
            Hashtable table = new Hashtable();
            table.Add("SchoolMark", schoolMark);
            table.Add("SchoolYear",schoolYear);
            table.Add("Term",term);
            table.Add("Status",0);
            table.Add("ManagerID",userId);

            return (int)InsertCustomer("MoralityHistory.InsertNew",table);
        }


        /// <summary>
        /// 更新状态
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <param name="historyId"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public int UpdateStatus(string schoolMark,int historyId, int status)
        {
            Hashtable table = new Hashtable();
            table.Add("SchoolMark", schoolMark);
            table.Add("rid", historyId);
            table.Add("Status", status);

            return UpdateCustomer("MoralityHistory.UpdateStatus", table);
        }
    }
}